package com.example.service.impl;

import com.example.mapper.EmpExprMapper;
import com.example.mapper.EmpMapper;
import com.example.pojo.*;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

@Transactional
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Override
    public PageResult<Emp> page(EmpQueryParam param) {
        Long total = empMapper.count();
        Integer start = (param.getPage() - 1) * param.getPageSize();
        List<Emp> empList = empMapper.page(param);
        return new PageResult<>(total,empList);
        /*PageHelper.startPage(page,pageSize);
        List<Emp>empList=empMapper.list();
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<>(p.getTotal(),p.getResult());*/
    }
    // 新增员工信息
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.save(emp);
        List<EmpExpr>exprList=emp.getExprList();
        if(exprList!=null && exprList.size()>0){
            exprList.forEach(expr-> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.batchSave(exprList);
        }
    }
    // 查询所有员工
    @Override
    public List<Emp> findAll() {
        List<Emp> emps = empMapper.findAll();
        return emps;
    }
    // 根据id查询员工
    @Override
    public Emp findById(Integer id) {
        return empMapper.findById(id);
    }
    // 修改员工信息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Emp emp) {
        // 修改
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        // 删除
        empExprMapper.batchDelete(List.of(emp.getId()));
        // 添加
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList !=null && exprList.isEmpty()){
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
            empExprMapper.batchSave(exprList);
        }
    }
    // 批量删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<Integer> ids) {
        empMapper.Delete(ids);
        empExprMapper.batchDelete(ids);
    }

    @Override
    public JobOption getEmpJoData() {
        List<Map<String, Long>>jobs = empMapper.getEmpJoData();
        if (jobs != null && jobs.isEmpty()){
            List<String> jobList = jobs.stream().map(map -> String.valueOf(map.get("job_name"))) .toList();
            List<Long> dataList = jobs.stream().map(map -> map.get("count(*)")).toList();
            return new JobOption(jobList,dataList);
        }
        return null;
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.login(emp);
        return e;
    }

    @Override
    public List<Map> getEmpGenderData() {
        return empMapper.getEmpGenderData();
    }
}
